﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Main.Common;

namespace Main.UI.Components
{
    public partial class FileMenuComponent : UserControl, IFileMenuComponent
    {
        
        public FileMenuComponent()
        {
            InitializeComponent();
        }

        private IFileMenuItem Active { get; set; }

        private void Init()
        {
            FileMenuItemList list = new FileMenuItemList();
            FlowPanel.Controls.Clear();
            foreach (IFileMenuItem item in list)
            {
                Label label = new Label();
                label.Text = item.Text;
                label.Size = new Size(194, 37);
                label.TextAlign = ContentAlignment.MiddleLeft;
                label.Cursor = System.Windows.Forms.Cursors.Hand;
                label.Font = new System.Drawing.Font("Bookman Old Style", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label.ForeColor = System.Drawing.SystemColors.Window;
                label.Tag = item;
                label.MouseHover += MouseHoverLabel;
                label.MouseEnter += MouseEnterLabel;
                label.MouseLeave += MouseLeaveLabel;
                label.Click += MouseClickLabel;
                label.Enabled = item.IsEnabled();
                FlowPanel.Controls.Add(label);
            }
            Active = null;
        }

        void MouseClickLabel(object sender, EventArgs e)
        {
            Label label = sender as Label;
            if (label == null) return;
            if (Active != null)
            {
                if (Active.Equals(label.Tag)) return;
                foreach (Control c in FlowPanel.Controls)
                {
                    if (Active.Equals(c.Tag)) { c.BackColor = System.Drawing.SystemColors.HotTrack; }
                }
                Active.Adjust(Visibility.Hide);
                SelectedItemPanel.Controls.Clear();
            }
            Active = (IFileMenuItem)label.Tag;
            label.BackColor = System.Drawing.SystemColors.ActiveCaption;
            Control target = (Control)Active;
            target.Dock = DockStyle.Fill;
            SelectedItemPanel.Controls.Add(target);
            Active.Adjust(Visibility.Show);
        }

        void MouseLeaveLabel(object sender, EventArgs e)
        {
            Label label = sender as Label;
            if (label == null) return;
            if (Active != null && Active.Equals(label.Tag))
            { return; }
            label.BackColor = System.Drawing.SystemColors.HotTrack;
        }

        void MouseEnterLabel(object sender, EventArgs e)
        {
            Label label = sender as Label;
            if (label == null) return;
            if (Active != null && Active.Equals(label.Tag))
            { return; }
            label.BackColor = System.Drawing.SystemColors.Highlight;
        }

        void MouseHoverLabel(object sender, EventArgs e)
        {
            Label label = sender as Label;
            if (label == null) return;
            if (Active != null && Active.Equals(label.Tag))
            { return; }
        }


        public void Adjust(Visibility value)
        {
            if (value == Visibility.Show)
            {
                if(FlowPanel.Controls.Count == 0)
                    Init();
                this.Show();
            }
            if (value == Visibility.Hide)
            {
                this.Hide();
                Parent.Controls.Remove(this);
                Services.ComponentManager.RibbonBar.Adjust(Visibility.Show);
                Services.ComponentManager.StudioDock.Adjust(Visibility.Show);
                SelectedItemPanel.Controls.Clear();
            }
        }
    }
}
